/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pictavore.util;


import java.io.File;
import java.awt.*;
import javax.swing.*;

/**
 *
 * @author dvenable
 */
// <editor-fold defaultstate="collapsed" desc="Useful utilitis">
public class Utils
{

    public final static String jpeg = "jpeg";
    public final static String jpg = "jpg";
    public final static String gif = "gif";
    public final static String tiff = "tiff";
    public final static String tif = "tif";
    public final static String png = "png";
    public final static String fits = "fits";

    /*
     * Get the extension of a file.
     */
    public static String getExtension(File f)
    {
        String s = f.getName();
        return getExtension(s);
    }

    public static String getExtension(String s)
    {
        String ext = null;
        int i = s.lastIndexOf('.');
        if (i < s.length() - 1)
        {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }

    public static String getFilename(String s)
    {

        int i = s.lastIndexOf(File.pathSeparatorChar);
        if (i < 0)
        {
            return s;

        }
        return s.substring(i + 1);
    }

    public static String getFilenameWithoutExtension(String s)
    {

        int i = s.lastIndexOf(File.separatorChar);
        int first = (i < 0) ? 0 : i + 1;
        i = s.lastIndexOf('.');
        int last = (i < 0 || i < first) ? s.length() : i;
        return s.substring(first, last);
    }

    public static Image LoadImage(String path, JFrame p)
    {
        Image img = p.getToolkit().getImage(path);
        try {
            MediaTracker tracker = new MediaTracker(p);
            tracker.addImage(img, 0);
            tracker.waitForID(0);
        } catch (Exception e) {}
        return img;
    }

    /*public static BufferedImage ReadFits(String path)
    {
    try
    {
    Fits f = new Fits(path);
    ImageHDU hdu = (ImageHDU)f.readHDU();

    int[] dims = hdu.getAxes();
    if (dims.length != 2)
    throw new Exception("Only supports 2-dimensional FITS images");
    int nbits = hdu.getBitPix();
    int w = dims[0];
    int h = dims[1];
    System.out.println("Dims: " + dims[0] + " x " + dims[1]);
    System.out.println("BitDepth: " + nbits);

    Object data = hdu.getKernel();
    String cname = data.getClass().getName();

    if (cname.charAt(0) != '[' || cname.charAt(1) != '[')
    throw new Exception("FITS data is not an appropriate array");

    switch (cname.charAt(2))
    {
    case 'B'://byte array
    break;
    case 'S'://short array
    break;
    case 'I'://int array
    break;
    case 'J'://long array
    break;
    case 'F'://float array
    break;
    case 'D'://double array
    break;
    default:
    throw new Exception("FITS data is not valid format");
    }

    short[][] words = null;

    int max = 0;
    int min = Short.MAX_VALUE;
    for (int j=0; j<h; ++j)
    for (int i=0; i<w; ++i)
    {
    int v = words[i][j];
    max = (v > max) ? v : max;
    min = (v<min) ? v : min;
    }

    System.out.println("Max value = " + max);
    System.out.println("Min value = " + min);


    BufferedImage bb = new BufferedImage(w,h, BufferedImage.TYPE_USHORT_GRAY);
    WritableRaster rst = bb.getRaster();
    DataBufferUShort dbuf = (DataBufferUShort)rst.getDataBuffer();
    short[] pixels = (short[])dbuf.getData();
    //            ColorModel ucm = bb.getColorModel();
    //            ColorModel cm2 = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[]{16}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
    //            bb = null;
    //
    //            DataBufferUShort db = new DataBufferUShort(w*h);
    //            short[] pixels = (DataBufferUShort)db.getData();
    for (int j=0, k=0; j<h; ++j)
    for (int i=0; i<w; ++i)
    {
    int v = words[i][j];
    pixels[k] = (short)((v & 0x000000FF) << 8);
    pixels[k] |= (short)((v>>8) & 0x000000FF);
    //                    pixels[k] = (short)50000;
    k++;
    }


    //            WritableRaster rst = Raster.createPackedRaster(db, w, h, nbits, null);
    //            WritableRaster rst2 = Raster.createPackedRaster(DataBuffer.TYPE_USHORT, w, h, 1, nbits, null);
    //            BufferedImage bi = new BufferedImage(cm2, rst2, false, null);
    return bb;


    } catch (Exception ex)
    {
    System.out.println("Error: " + ex.getMessage());
    ex.printStackTrace();
    return null;
    }
    }*/}
// </editor-fold>

